﻿
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace xknpoi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       // private string exclePath = @"C:\core\20160219城东所天网二期堪点总表.xlsx";
        private void button1_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "Excel|*.xls;*.xlsx";
            if (file.ShowDialog() == DialogResult.OK)
            {
                string exclePath = file.FileName;
                List<string> listpath = new List<string> { };
                string savePath = Path.Combine("c:\\", "pic");
                if (!Directory.Exists(savePath))//判断是否存在保存文件夹，没有则新建
                    Directory.CreateDirectory(savePath);

                bool result = ExcelToImage(exclePath, savePath, ref listpath);
                if (result)
                    MessageBox.Show("成功");
                else
                    MessageBox.Show("失败");
            }
        }

 
        #region 从excel里获取图片
        /// <summary>
        /// 从excel获取图片
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <param name="savepath">图片保存路径</param>
        /// <param name="listPath">返回保存的图表地址list</param>
        /// <returns>保存图片是否成功</returns>
        private bool ExcelToImage(string filepath, string savepath, ref List<string> listPath)
        {
            try
            {
                using (FileStream fsReader = File.OpenRead(filepath))
                {
                     XSSFWorkbook wk = new XSSFWorkbook(fsReader);

                     //  HSSFWorkbook wk = new HSSFWorkbook(fsReader);
                
                   for ( int j = 1; j < wk.Count; j++)
                   {
                       XSSFSheet xs = (XSSFSheet)wk.GetSheetAt(j);  
                       IList pictures1= xs.GetAllPictureInfos();

                     //  string s = xs.GetCellComment(2, 2).ToString();
                       ISheet sheet = wk.GetSheetAt(j);   //读取当前表数据
                       string name = "";
                       if (sheet.GetRow(1).Cells[1]!=null)
	                    {
                            name = sheet.GetRow(1).Cells[1].ToString();
	                    } 
                       //StringBuilder sbr = new StringBuilder();
                       //for (int c = 0; c <= sheet.LastRowNum; c++)  //LastRowNum 是当前表的总行数
                       //{
                       //    IRow row = sheet.GetRow(j);  //读取当前行数据
                       //    if (row != null)
                       //    {
                       //        sbr.Append("-------------------------------------\r\n"); //读取行与行之间的提示界限
                       //        for (int k = 0; k <= row.LastCellNum; k++)  //LastCellNum 是当前行的总列数
                       //        {
                       //            ICell cell = row.GetCell(k);  //当前表格
                       //            if (cell != null)
                       //            {
                       //                sbr.Append(cell.ToString());   //获取表格中的数据并转换为字符串类型
                       //            }
                       //        }
                       //    }
                       //}
       
                 //   IList pictures = wk.GetAllPictures();
                    
               
                  

                        foreach (PicturesInfo pic in pictures1)
                       {
                            Image jpg = Image.FromStream(new MemoryStream(pic.PictureData));//从pic.Data数据流创建图片
                            string path = Path.Combine(savepath, string.Format("{0}.jpeg", name));
                            jpg.Save(path);//保存
                        }

                   }
                }
            }
            catch (Exception ex)
            {
                return false;

            }
            return true;
        }
        #endregion            
    }
}
